package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理文章分类 业务模块功能
 */
@Api(tags = "文章分类管理")
@RestController
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 插入分类
     * ~
     * @return
     */
    @ApiOperation("文章分类列表查询")
    @GetMapping("/admin/content/category/list")
    public Result<PageResult<ArticleCategoryVO>> pageList(){
        log.info("查询文章列表");
        return new Result<>(categoryService.pageList());
    }
    @ApiOperation( "新增分类")
    @PostMapping("/admin/content/category/insert")
    public Result<Void> addCategory(@RequestBody ArticleCategoryParam param){
        try{
            log.info("新增分类{}",param);
            categoryService.addCategory(param);
            return Result.success();
        }catch (Exception e){
            return null;
        }
    }
    @ApiOperation("修改文章分类")
    @PostMapping("/admin/content/category/update")
    public Result<Void> updateCategory(@RequestBody ArticleCategoryParam param){
        try{
            log.info("修改文章分类{}",param);
            categoryService.updateCategory(param);
            return Result.success();
        }catch (Exception e){
            return null;
        }
    }
    @ApiOperation("删除文章分类")
    @PostMapping("/admin/content/category/delete/{id}")
    public Result<Void> deleteCategory(@PathVariable("id") Integer id){
        try{
            log.info("删除文章分类{}",id);
            categoryService.deleteCategory(id);
            return Result.success();
        }catch (Exception e){
            return null;
        }
    }
    @ApiOperation("查询文章")
    @GetMapping("/admin/content/category/get")
    public Result<List<ArticleCategoryVO>> getCategory(ArticleCategoryQuery query){
        try{
            log.info("查询文章{}",query);
            List<ArticleCategoryVO> categoryVO = categoryService.getCategory(query);
            log.info("查询结果{}",categoryVO);
            return new Result<>(categoryVO);
        }catch (Exception e){
            return null;
        }
    }
}
